package com.aracky.system.service.impl;

import com.aracky.common.util.ConvertUtil;
import com.aracky.common.vo.PageVO;
import com.aracky.system.constants.ComConstants;
import com.aracky.system.mapper.SysMenuMapper;
import com.aracky.system.model.entity.SysBtnConfig;
import com.aracky.system.model.entity.SysMenu;
import com.aracky.system.model.form.SysMenuForm;
import com.aracky.system.model.request.SysMenuRequest;
import com.aracky.system.model.vo.SysMenuVO;
import com.aracky.system.service.SysBtnConfigService;
import com.aracky.system.service.SysMenuService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 菜单权限表 服务实现类
 * </p>
 *
 * @author zzp
 * @since 2022-07-14 11:22:57
 */
@Service
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements SysMenuService {


    @Resource
    private SysMenuMapper sysMenuMapper;

    @Autowired
    private SysBtnConfigService sysBtnConfigService;

    @Override
    public PageVO<SysMenuVO> getMenuPage(SysMenuRequest sysMenuRequest) {
        IPage<SysMenuVO> page = sysMenuMapper.findMenu(sysMenuRequest.toPage(), sysMenuRequest);
        return PageVO.convertPage(page);
    }

    @Override
    public List<SysMenuVO> getMenuList() {
        List<SysMenuVO> menuList = sysMenuMapper.findMenu(new SysMenuRequest());
        return menuList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveMenu(SysMenuForm sysMenuForm) {
        //先保存菜单信息
        SysMenu sysMenu = ConvertUtil.sourceToTarget(sysMenuForm, SysMenu.class);
        this.save(sysMenu);

        //判断是否为按钮，如果是按钮，保存配置信息
        if (ComConstants.MENU.TYPES[2].equals(sysMenu.getMenuType())) {
            SysBtnConfig sysBtnConfig = ConvertUtil.sourceToTarget(sysMenuForm, SysBtnConfig.class);
            sysBtnConfig.setBtnId(sysMenu.getMenuId());
            sysBtnConfig.setParentId(sysMenu.getParentId());
            sysBtnConfigService.save(sysBtnConfig);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateMenu(SysMenuForm sysMenuForm) {
        //更新菜单信息
        SysMenu sysMenu = ConvertUtil.sourceToTarget(sysMenuForm, SysMenu.class);
        this.updateById(sysMenu);

        //判断是否为按钮，如果是按钮，修改配置信息
        if (ComConstants.MENU.TYPES[2].equals(sysMenu.getMenuType())) {
            SysBtnConfig sysBtnConfig = ConvertUtil.sourceToTarget(sysMenuForm, SysBtnConfig.class);
            sysBtnConfig.setBtnId(sysMenu.getMenuId());
            sysBtnConfig.setParentId(sysMenu.getParentId());
            sysBtnConfigService.updateById(sysBtnConfig);
        }

    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteMenu(List<Long> menuIds) {

        this.removeBatchByIds(menuIds);

        sysBtnConfigService.removeBatchByIds(menuIds);

    }
}
